package com.kzd.gtboon.main.game

import android.Manifest
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import com.kzd.gtboon.R
import com.kzd.gtboon.base.BaseViewModel
import com.kzd.gtboon.databinding.ActivityGameVipBinding
import com.kzd.gtboon.entity.GameVip
import com.kzd.gtboon.main.KEY_GAME_DETAILS_TITLE
import com.kzd.gtboon.main.KEY_GAME_DETAILS_VIP
import com.kzd.gtboon.main.KEY_GAME_DETAILS_VIP_STR
import com.kzd.gtboon.main.game.adapter.GameVipAdapter
import com.kzd.gtboon.main.popup.ServiceCodePopup
import com.kzd.gtboon.utils.JsonUtils
import com.kzd.gtboon.utils.saveImageToGallery
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class GameVipActivity : OwnerViewBindingActivity<ActivityGameVipBinding>() {

    private val mServiceCodePopup: ServiceCodePopup by lazy {
        ServiceCodePopup(context = this) { _, type ->
            requestDangerousPermissions(arrayOf(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ), requestCode = type)
        }
    }

    private val mGameVipAdapter = GameVipAdapter()

    private val mTitle: String
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_TITLE) ?: ""

    private val mGameVip: MutableList<GameVip>
        get() = try {
            val vipStr = intent?.extras?.getString(KEY_GAME_DETAILS_VIP)
                ?: throw Exception()
            JsonUtils.stringToList(vipStr, GameVip::class.java)
        } catch (e: Exception) {
            mutableListOf()
        }

    private val mVipStr: String?
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_VIP_STR)

    private val mViewModel:BaseViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityGameVipBinding
        get() = ActivityGameVipBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flGameVipBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {
        mViewBinding.flGameVipBar.setTitleTextContent(mTitle)
        mViewBinding.rvGameVipBody.adapter = mGameVipAdapter

        mViewBinding.flGameVipBar.setOnClickRightTextView {
            val image = mViewModel.userServiceLiveData.value
                ?.data?.data?.qy_wx_img
            mServiceCodePopup.setContent(if (image.isNullOrEmpty()) {
                R.mipmap.ic_40008
            } else image, 1)
                .showPopupWindow()
        }

    }

    override fun initData() {
        mGameVipAdapter.setNewInstance(mGameVip)
        mVipStr?.let { mViewBinding.tvGameVipHint.text = it }
        mViewModel.userService()
    }

    override fun handlePermissionResult(requestCode: Int, granted: Boolean): Boolean {
        if (!granted) {
            toast("请到应用设置中开启存储权限！")
            return false
        }
        val bitmap = BitmapFactory.decodeResource(resources,
            if (requestCode == 1) R.mipmap.ic_40008 else R.mipmap.ic_40007)
        val fileName = "${if (requestCode == 1) R.mipmap.ic_40008 else R.mipmap.ic_40007}.png"
        if (saveImageToGallery(this@GameVipActivity, bitmap, fileName)) {
            toast("已保存")
        } else {
            toast("保存失败")
        }
        return super.handlePermissionResult(requestCode, granted)
    }
}